package xyz.ylx.proxy.methodproxy;

import org.springframework.cglib.core.Signature;

public class ProxyFastClass {
    static Signature s0 = new Signature("saveSuper", "()V");
    static Signature s1 = new Signature("saveSuper", "(I)V");
    static Signature s2 = new Signature("saveSuper", "(L)V");

    // 获取代理方法的编号
    /*
        Target
            save()          0
            save(int)       1
            save(long)      2
        signature 包括方法名字、参数返回值
     */
    public int getIndex(Signature signature) {
        if (s0.equals(signature)) {
            return 0;
        } else if (s1.equals(signature)) {
            return 1;
        } else if (s2.equals(signature)) {
            return 2;
        }

        return -1;
    }

    // 根据方法编号，正常调用目标对象方法
    public Object invoke(int index, Object proxy, Object[] args) {
        if (index == 0) {
            ((Proxy) proxy).saveSuper();
        } else if (index == 1) {
            int i = (int) args[0];
            ((Proxy) proxy).saveSuper(i);
        } else if (index == 2) {
            long j = (long) args[0];
            ((Proxy) proxy).saveSuper(j);
        } else {
            throw new RuntimeException("无此方法");
        }

        return null;
    }
}
